package com.ebb;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.stackmob.sdk.api.StackMob;
import com.stackmob.sdk.callback.StackMobCallback;
import com.stackmob.sdk.exception.StackMobException;

public class EBBSignUpActivity extends Activity implements OnClickListener {
	private EditText etId;
	private EditText etPwd;
	private EditText etNick;
	private Button btnSignUp;
	private Button btnCancel;
	private StackMob stackmob;
	private static final String TAG = EBBStackMobActivity.class.getCanonicalName();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signup);
		Log.i(TAG, "#Sign Up :: onCreate");

        EBBAlert.setContext(EBBSignUpActivity.this);
        EBBAlert.setDuration(Toast.LENGTH_SHORT);
		
		stackmob = ((EBBStackMobApplication) getApplication()).getStackMobService();
		
		initLayout();
	}
	
	@Override
	protected void onStart() {
		super.onStart();
		Log.i(TAG, "#Sign Up :: onStart");
	}

	@Override
	protected void onStop() {
		super.onStop();
		Log.i(TAG, "#Sign Up :: onStop");
		etId.setText("");
		etPwd.setText("");
		etNick.setText("");
	}
	
	public void initLayout() {
		this.etId = (EditText) findViewById(R.id.id_value);
		this.etPwd = (EditText) findViewById(R.id.pwd_value);
		this.etNick = (EditText) findViewById(R.id.nick_value);

		this.btnSignUp = (Button) findViewById(R.id.signup_button);
		this.btnCancel = (Button) findViewById(R.id.cancel_button);
		this.btnSignUp.setId(0x2001);
		this.btnCancel.setId(0x2002);
		this.btnSignUp.setOnClickListener(this);
		this.btnCancel.setOnClickListener(this);
	}

	public void onClick(View v) {
		String username = etId.getText().toString();
		String password = etPwd.getText().toString();
		String nickname = etNick.getText().toString();
		
		switch (v.getId()) {
		case 0x2001: // Sign Up
			if (username.length() > 0 && password.length() > 0 && nickname.length() > 0) {
				Map<String, String> params = new HashMap<String, String>();
				params.put("username", username);
				params.put("password", password);
				params.put("nickname", nickname);
				stackmob.post("user", params, new StackMobCallback() {
					@Override
					public void success(String e) {
    					runOnUiThread(new Runnable() {
    						@Override
    						public void run() {
    							EBBAlert.message(1, null, "Sign Up Success!");
    						}
    					});
						Log.i(TAG, "Sign Up Success: " + e);
					}
					
					@Override
					public void failure(StackMobException e) {
						EBBAlert.message(0, "Sign Up Failure", e.getMessage());
						Log.i(TAG, "Sign Up Failure: " + e.getMessage());
					}
				});
				finish();
			} else {
				EBBAlert.message(0, "Sign Up Failure", "Please insert a ID/Password/Nickname !");
			}
			break;
		case 0x2002: // Back
			finish();
			break;
		}
	}
}